<template>
  <!--  公话-综合查询- 话单记录 -->
  <div class="query_of_consumption_records">
    <el-form :inline="true" :model="elform" class="demo-form-inline" size="mini">
      <el-form-item label="姓名:">
        <el-input v-model="userName" placeholder="姓名" size="mini" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label="设备编号:">
        <el-input
          v-model="terminalCode"
          placeholder="设备编号"
          size="mini"
          clearable
          style="width: 120px"
        ></el-input>
      </el-form-item>
      <el-form-item label="开始日期:">
        <el-date-picker
          v-model="datetime[0]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[0]"
          value-format="HH:mm:ss"
          placeholder="开始时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item label="结束日期:">
        <el-date-picker
          v-model="datetime[1]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="结束日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[1]"
          value-format="HH:mm:ss"
          placeholder="结束时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="gosearch" size="mini" icon="el-icon-search">查询</el-button>
        <el-button type="primary" @click="exportexeclto" size="mini">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="agentlist"
      style="width: 100%"
      border="border"
      class="tablebox"
      highlight-current-row
      :header-cell-style="headFirst"
      :cell-style="(res) => headFirst_col(res, [])"
    >
      <el-table-column prop="cardNumber" label="卡号" min-width="100px"></el-table-column>
      <el-table-column prop="userName" label="姓名"></el-table-column>
      <!-- <el-table-column prop="unitName" label="学校"></el-table-column> -->
      <el-table-column prop="terminalCode" label="设备编号" min-width="130px"></el-table-column>
      <el-table-column prop="startTime" label="通话开始时间" min-width="150px"></el-table-column>
      <el-table-column prop="calledDuration" label="通话时长"></el-table-column>
      <el-table-column prop="userFreeMinutes" width="110px" label="免费时长(分钟)"></el-table-column>
      <el-table-column prop="chargeDuration" width="110px" label="计费时长(分钟)"></el-table-column>
      <el-table-column width="110px" label="消费金额(元)">
        <template slot-scope="scope">
          <div>{{ scope.row.userChargeAmount | amountyuan }}</div>
        </template>
      </el-table-column>
      <el-table-column width="120px" label="消费后剩余金额(元)">
        <template slot-scope="scope">
          <div>{{ scope.row.userAmountAfter | amountyuan }}</div>
        </template>
      </el-table-column>
      <el-table-column width="110px" prop="calledPhoneNumber" label="呼叫的电话号码" min-width="100px"></el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChangepage"
        @current-change="handleCurrentChangepage"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="recordCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      elform: {},
      dataListLoading: false,
      messageType: 0,
      //当前id
      userGuid: '',
      //权限验证
      identity: '',
      //学校id
      schoolid: '',
      //学校列表
      schooloptions: [],
      //设备编号
      terminalCode: '',
      // 姓名
      userName: '',
      //时间
      datetime: [],
      time: [],
      //列表
      agentlist: [],
      //页数
      pageNum: 1,
      pageSize: 20,
      recordCount: 0,
      currentPage: 1,
      searchInfo: {},
      exportpam: {},
      agentlistout: []
    }
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid
      }
    }
  },
  created() {
    this.datetime = [this.getNowTime('yy-mm-dd'), this.getNowTime('yy-mm-dd')]
    this.time = ['00:00:00', '23:59:59']
  },
  methods: {
    gosearch() {
      // this.pageNum = 1;
      this.searchInfo = {}
      if (this.terminalCode !== null && this.terminalCode !== '') {
        this.searchInfo.terminalCode = this.terminalCode
      }
      this.searchInfo.startTime = this.datetime[0] + ' ' + this.time[0]
      this.searchInfo.endTime = this.datetime[1] + ' ' + this.time[1]
      if (this.userName !== null && this.userName !== '') {
        this.searchInfo.userName = this.userName
      }
      this.searchInfo.unitGuid = this.changeschoolid
      this.agentlistinit()
    },
    //获取代理商话单列表
    agentlistinit() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/voip/bill/search'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          this.agentlist = data.messageContent.data || []
          //消费金额
          this.recordCount = data.messageContent.recordCount
          this.currentPage = data.messageContent.pageNum
          this.dataListLoading = false
          if (this.agentlist.length == 0) {
            this.$message({
              message: '查询成功当前暂无数据',
              type: 'success'
            })
          }
        }
      })
    },
    // 导出功能
    exportexeclto() {
      if (this.agentlist.length == 0) {
        this.$message({
          message: '查询数据后导出',
          type: 'warning'
        })
        return false
      }
      this.outexecel()
    },
    outexecel() {
      this.$http({
        url: this.$http.adornUrl('/voip/bill/search'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageNum: 1,
            pageSize: this.recordCount,
            searchInfo: this.searchInfo
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          this.agentlistout = data.messageContent.data
          for (var i = 0; i < this.agentlistout.length; i++) {
            this.agentlistout[i].userChargeAmount = this.amountyua(
              this.agentlistout[i].userChargeAmount
            )
            this.agentlistout[i].userAmountAfter = this.amountyua(
              this.agentlistout[i].userAmountAfter
            )
            //   this.agentlistout[i].adjusted = this.amountjia(
            //     this.agentlistout[i].adjusted
            //   );
            //   this.agentlistout[i].adjustedBalance = this.amountjia(
            //     this.agentlistout[i].adjustedBalance
            //   );
          }
          setTimeout(() => {
            this.export2Excel()
          }, 0.1)
        }
      })
    },
    export2Excel() {
      const nowtime = this.getimeto()
      require.ensure([], () => {
        const multiHeader = []
        const multiHeader2 = []
        const merges = []
        const { export_json_to_excel } = require('../../../excel/Export2Excel')
        const tHeader = [
          '卡号',
          '姓名',
          '设备编号',
          '通话开始时间',
          '通话时长',
          '免费时长(分钟)',
          '计费时长(分钟)',
          '消费金额(元)',
          '消费后剩余金额(元)',
          '呼叫的电话号码'
        ]
        const filterVal = [
          'cardNumber',
          'userName',
          'terminalCode',
          'startTime',
          'calledDuration',
          'userFreeMinutes',
          'chargeDuration',
          'userChargeAmount',
          'userAmountAfter',
          'calledPhoneNumber'
        ]
        const list = this.agentlistout
        const data = this.formatJson(filterVal, list)
        export_json_to_excel({
          multiHeader, //这里是第一行的表头
          multiHeader2, //这里是第二行的表头
          header: tHeader, //这里应该是算第三行的表头
          data,
          merges,
          filename: '话单记录' + nowtime
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleSizeChangepage(val) {
      this.pageSize = val
      this.gosearch()
    },
    handleCurrentChangepage(val) {
      this.pageNum = val
      this.gosearch()
    }
  }
}
</script>

<style lang="scss" scoped>
.query_of_consumption_records {
  padding: 10px;

  /deep/.el-date-editor.el-input,
  .el-date-editor.el-input__inner {
    width: 180px;
  }

  ::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }

  //滚动条的滑块
  ::-webkit-scrollbar-thumb {
    background-color: #a1a3a9;
    border-radius: 3px;
  }
  .demo-form-inline,
  .demo-form-inline1 {
    /deep/.el-form-item__label {
      padding-right: 5px;
    }
    .el-form-item {
      margin-bottom: 5px;
    }
    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 250px;
    }
  }
   .page {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
